/**
 * Created by DukeCrushIt on 2015/8/24.
 */
//module game{
    class  AnimationController{
        private armatures:Object;
        public static NORMAL_ANIMATIONS:string[] = ["xilian","daheqian","dongerduo","zhayanjing","yaoweiba","naoyangyang","shuijiao","jiao"];
        public static PROGRESS_ANIMATION:string[] = ["paxia","qilai"];
        public static FUMO_ANIMATION:string = "fumo";
        public frameItem:FrameItem;
        public name:string;
        public animationQueue:Object;

        public constructor(){
            //super();
            this.armatures = {}
            this.animationQueue = {};
            this.name = "animationcontroller";
            this.frameItem = new FrameItem();
            this.frameItem.callFun = this.update;
            this.frameItem.callObj = this;
        }

        private checkTempDisplay(name:string){
            if( this.tempSlotData[name] ){
                var list = this.tempSlotData[name];
                var len = list.length;
                var info;
                while(list.length){
                    info = list.pop();
                    this.setSlotDisplay(name,info[0],info[1],info[2])
                }
                delete this.tempSlotData[name];
            }
        }

        public gotoAndPlay(name,state, playtimes,force=false){
            var armature:dragonBones.Armature = this.armatures[name];
            if( armature != undefined ){
                armature.animation.gotoAndPlay(state,0,-1,playtimes);
                this.checkTempDisplay(name);
                if( state == "jiao" || state == "shuijiao") {
                    if( PetMgr.getInstance().curPet.type == Model.GOOD_TYPE_CAT){
                        SoundMgr.getInstance().playEffect(SoundMgr.MIOW_1);
                    }else{
                        SoundMgr.getInstance().playEffect(SoundMgr.BARK_1);
                    }
                }
                //else if( state == "shuijiao"){
                //    if( PetMgr.getInstance().curPet.type == Model.GOOD_TYPE_CAT){
                //        SoundMgr.getInstance().playEffect(SoundMgr.SNORING);
                //    }else{
                //        SoundMgr.getInstance().playEffect(SoundMgr.BREATH);
                //    }
                //}
                //else if( state == "daheqian"){
                    //if( PetMgr.getInstance().curPet.type == Model.GOOD_TYPE_CAT){
                    //    SoundMgr.getInstance().playEffect(SoundMgr.MIOW_3);
                    //}else{
                    //    SoundMgr.getInstance().playEffect(SoundMgr.BARK_3);
                    //}
                //}
            }
        }
        public wakeUp(petname:string){
            var armature:dragonBones.Armature = this.armatures[petname];
            if( armature != undefined  && armature.animation.lastAnimationName == "shuijiao"){
                this.gotoAndPlay(petname,"qilai",1);
            }
        }
        public update(delta:number){
            dragonBones.WorldClock.clock.advanceTime( 0.02);
            var armature:dragonBones.Armature;
            var playTimes:number = 1;
            var randonIdx:number;
            var targetAnimationName:string;
            var anim:string;
            for(var key in this.armatures){
                armature = this.armatures[key];
                if( armature.animation.isCompleted ){
                    if( armature.animation.lastAnimationName == AnimationController.NORMAL_ANIMATIONS[6]){
                        this.gotoAndPlay(key,AnimationController.PROGRESS_ANIMATION[1],1);
                    }else if(armature.animation.lastAnimationName == AnimationController.PROGRESS_ANIMATION[0]){
                        playTimes = Math.ceil(Math.random()*5);
                        this.gotoAndPlay(key,AnimationController.NORMAL_ANIMATIONS[6],playTimes);
                    }else{
                        randonIdx = Math.ceil(Math.random()*7);
                        targetAnimationName = AnimationController.NORMAL_ANIMATIONS[randonIdx];
                        if( targetAnimationName == AnimationController.NORMAL_ANIMATIONS[6]){
                            this.gotoAndPlay(key,AnimationController.PROGRESS_ANIMATION[0],1);
                        }else{
                            this.gotoAndPlay(key,targetAnimationName,1);
                        }
                    }
                }
            }
        }

        public control(key,armature){
            if(this.armatures[key] == undefined){
                this.armatures[key] = armature;
                this.animationQueue[key] = [];
            }
            armature.getSlot("tou").display.visible = false;
            armature.getSlot("bozi").display.visible = false;
            armature.getSlot("yanjing").display.visible = false;
        }

        public getDisplay(key):any{
            if( this.armatures[key] )
                return this.armatures[key].display;
            return null
        }

        public uncontroll(key){
            if( this.armatures[key] != undefined){
                delete this.armatures[key];
                delete this.animationQueue[key];
            }
        }
        /** װ��������ʾ **/
        public defaultState = {"tou":false,"yanjing":false,"bozi":false};//Ŀǰ�ṩ��װ��
        //public defaultTexture = {};
        private tempSlotData:Object = {};
        public setSlotDisplay(armatueName:string,slot:string,visible:boolean,texture:egret.Texture=null){
            var armature:dragonBones.Armature = this.armatures[armatueName];
            if( armature ) {
                armature.getSlot(slot).display.visible = visible;
                if( visible){
                    armature.getSlot(slot).display.texture = texture;
                }
            }else{
                if( !this.tempSlotData[armatueName])
                    this.tempSlotData[armatueName] = [];
                this.tempSlotData[armatueName].push([slot,visible,texture]);
            }
            //if( !this.defaultTexture[armatueName]){
            //    this.defaultTexture[armatueName] = {};
            //    this.defaultTexture[armatueName]["tou"] = armature.getSlot("tou").display.texture;
            //    this.defaultTexture[armatueName]["yanjing"] = armature.getSlot("yanjing").display.texture;
            //    this.defaultTexture[armatueName]["bozi"] = armature.getSlot("bozi").display.texture;
            //}

        }

        private static _instance:AnimationController;
        public static getInstance():AnimationController{
            if( AnimationController._instance == null )
                AnimationController._instance = new AnimationController();
            return AnimationController._instance;
        }
    }
//}